using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.Web;
using CrystalDecisions.Shared;
using System.Data.SqlClient;
using Mani.Common;
using Ghasam.Business.Services;
using Ghasam.Business.Domain.Entity;
using System.Collections.Generic;

public partial class InvoicePage : BasePage
{

    private IInvoiceService invoiceService;
    private IGoodsService goodsService;
    private CrystalReportManager manager;

    public IGoodsService GoodsService
    {
        get { return goodsService; }
        set { goodsService = value; }
    }
    public IInvoiceService InvoiceService
    {
        get { return invoiceService; }
        set { invoiceService = value; }
    }
	
    public override string ActionName {
        get {
            return "vw_invoice";
        }
    }
    Invoice example = new Invoice();
    DateTime startDate = DateTime.Now.AddDays(-7);
    DateTime endDate = DateTime.Now;
    protected void Page_Load( object sender, EventArgs e ) {

        if (!Page.IsPostBack) {
            GetInvoices(0, true);
            CheckCustomerType();
        }
    }

    private void CheckCustomerType() {
        bool show = CommonHelper.ShowHiddenCustomers();
        gvInvoice.Columns[3].Visible = show;
        divHiddenCustomers.Visible = show;
    }
    private void GetValues()
    {
        if (!String.IsNullOrEmpty(txtStartDate.Text))
            startDate = MyPersianDate.PersianToDateTime(txtStartDate.Text);

        if (!String.IsNullOrEmpty(txtEndDate.Text))
            endDate = MyPersianDate.PersianToDateTime(txtEndDate.Text);

        if (!divHiddenCustomers.Visible || rbFormalCustomers.Checked)
            example.IsFormal = true;
        else if (divHiddenCustomers.Visible && rbHiddenCustomers.Checked)
            example.IsFormal = false;

        if (ddlGoods.SelectedValue != "-1")
            example.Goods = goodsService.GetGoods(Convert.ToInt32(ddlGoods.SelectedValue));
    }
    private void GetInvoices(int pageNumber, bool setPageCount)
    {
        GetValues();
        if (setPageCount)
            gvInvoice.VirtualItemCount = invoiceService.CountInvoices(example);
        gvInvoice.DataSource = invoiceService.FindInvoices(example, 15, pageNumber, startDate, endDate);
        gvInvoice.DataBind();

    }
    private void GetSortingValues(int pageNumber, bool setPageCount,string sortParam, bool direction)
    {
        GetValues();
        if (setPageCount)
            gvInvoice.VirtualItemCount = invoiceService.CountInvoices(example);
        gvInvoice.DataSource = invoiceService.SortInvoice(example,15, pageNumber, startDate, endDate, sortParam, direction);
        gvInvoice.DataBind();

    }
    protected void CustomerTypeCheckedChanged( object sender, EventArgs e ) {
        gvInvoice.DataBind();
    }

    protected void btnSearch_Click( object sender, EventArgs e ) {
        GetInvoices(0, true);
    }

    protected void gvInvoice_SelectedIndexChanged( object sender, EventArgs e ) {
        int invoiceId = Convert.ToInt32(gvInvoice.DataKeys[gvInvoice.SelectedIndex].Value);
        Response.Redirect("rpt/InvoiceReport.aspx?InvoiceId=" + invoiceId.ToString());
    }

    protected void gvInvoice_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvInvoice.PageIndex = e.NewPageIndex;
        GetInvoices(e.NewPageIndex, false);
    }
    static int numberDiv = 0;
    protected void gvInvoice_Sorting(object sender, GridViewSortEventArgs e)
    {
        bool dir;
        if ((numberDiv % 2) == 0)
            dir = true;
        else
            dir = false;
        numberDiv++;
        GetSortingValues(0, true, e.SortExpression, dir);
    }
}
